//
//  DYZExistingCarController.m
//  DYZCars
//
//  Created by 杨瑞 on 16/8/19.
//  Copyright © 2016年 MF. All rights reserved.
//

#import "DYZExistingCarController.h"
#import "DYZOwnCarCellCell.h"
#import "DYZCarModel.h"
#import "UIColor+Addition.h"
#import "DYZCarDetailInfoController.h"

@interface DYZExistingCarController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,weak) UILabel *headerLabel;

@end

@implementation DYZExistingCarController

- (void)viewDidLoad {
    [super viewDidLoad];
    //navigation的title
//    self.navigationItem.title = @"现车";
    //设置tableView
    UITableView *tableView = [[UITableView alloc]init];
    [self.view addSubview:tableView];
    //设置数据源
    tableView.dataSource = self;
    //设置代理
    tableView.delegate = self;
    //约束
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    //注册单元格
    //    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellid"];
    [tableView registerNib:[UINib nibWithNibName:@"DYZOwnCarCellCell" bundle:nil] forCellReuseIdentifier:@"cellid"];
    self.cars = [self loadCarData];
    // Do any additional setup after loading the view, typically from a nib.
}
-(NSArray *)loadCarData{
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"carsCategory.plist" withExtension:nil];
    NSArray *array = [NSArray arrayWithContentsOfURL:url];
    NSMutableArray *arrM = [NSMutableArray array];
    for (NSDictionary *dict in array) {
        DYZCarModel *car = [DYZCarModel loadDataWithDict:dict];
        [arrM addObject:car];
    }
    return arrM.copy;
}

//改变组头的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}
//改变cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc]init];
    headerView.backgroundColor = [UIColor colorWithHex:0xf0f0f0];
    UILabel *textLabel = [[UILabel alloc]init];
    [textLabel setFont:[UIFont systemFontOfSize:12]];
    [textLabel sizeToFit];
    [headerView addSubview:textLabel];
    [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.top.bottom.equalTo(headerView);
    }];
    //    if (section == 0) {
    //        textLabel.text = @"奔驰";
    //    }else if (section == 1){
    //        textLabel.text = @"宾利";
    //    }else{
    //        textLabel.text = @"宝马";
    //    }
    DYZCarModel *model = self.cars[section];
    textLabel.text = model.title;
    return headerView;
}
#pragma mark - UITableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    if ([self.navigationItem.title isEqualToString:@"宾利"]) {
        DYZCarDetailInfoController *carVC = [[DYZCarDetailInfoController alloc]init];
        DYZCarModel *model = self.cars[indexPath.section];
        carVC.navigationItem.title = model.car[indexPath.row];
        [self.navigationController pushViewController:carVC animated:YES];
//    }
}
#pragma mark - UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.cars.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //    if (section == 0) {
    //        return 5;
    //    }else if (section == 1) {
    //        return 3;
    //    }else{
    //        return 1;
    //    }
    DYZCarModel *model = self.cars[section];
    return model.car.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DYZOwnCarCellCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellid" forIndexPath:indexPath];
    DYZCarModel *model = self.cars[indexPath.section];
    cell.carCategoryLabel.text = model.car[indexPath.row];
    [cell.textLabel setFont:[UIFont systemFontOfSize:14]];
    [cell.textLabel setTextColor:[UIColor colorWithHex:0x333333]];
    return cell;
}
#pragma mark - 懒加载
-(NSArray *)cars{
    if (_cars == nil) {
        _cars = [NSArray array];
    }
    return _cars;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
